package server.layer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.Socket;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Awake
 * Date: 23.04.12
 * Time: 1:37
 * To change this template use File | Settings | File Templates.
 */
public class ServiceHandler {
    private ArrayList<Person> persons = new ArrayList<Person>();
    JsonUtil util = new JsonUtil();
    private static String DEFAULT_IMAGE = "E:/for_db/Pedobearseal.jpg";
    private PersonDao dao = new PersonDao();


    public void receive(String str, Socket socket) {
        try {
            JSONObject fullStr = new JSONObject(str);
            ServiceProtocol command = ServiceProtocol.getCommand(fullStr.getString("command"));
            switch (command) {
                case GET_DESCRIPTION:
                    int id = fullStr.getInt("argument");
                    sendImage(socket, id);
                    break;
                case CONNECT:
                    sendList(socket);
                    break;
                case UPDATE_IMAGE:
                    updateImage(fullStr);
                    break;
                case UPDATE_PERSON:
                    break;
            }

        } catch (JSONException e1) {
            e1.printStackTrace();
        }


    }

    public void sendList(Socket socket) {
        try {
            this.connectDB();
            JSONObject fullCommand = new JSONObject();
            JSONArray array = util.arrayToJson(persons);
            fullCommand.put("array", array);
            fullCommand.put("command", ServiceProtocol.UPDATE_LIST.getAbbr());
            ServerThread.server.sendMessageTo(socket, fullCommand.toString());

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void sendImage(Socket socket, int id) {

        try {
            byte[] image = dao.getImage(id);
            if (image != null) {
                JSONObject fullCommand = new JSONObject();
                fullCommand.put("command", ServiceProtocol.UPDATE_IMAGE.getAbbr());
                JSONArray jImg = new JSONArray(image);
                fullCommand.put("img", jImg);
                ServerThread.server.sendMessageTo(socket, fullCommand.toString());
            }


        } catch (SQLException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }


        /*
 PersonDao p = new PersonDao();
 BufferedImage img;
 try {
     String imgPath = p.getPerson(id).getImgName();
     imgPath = (imgPath != null && !imgPath.isEmpty()) ? imgPath : DEFAULT_IMAGE;
     img = ImageIO.read(new File(imgPath));
     ByteArrayOutputStream imageBuffer = new ByteArrayOutputStream();
     ImageIO.write(img, "JPG", imageBuffer);
     imageBuffer.flush();
     imageBuffer.close();
     byte[] data = imageBuffer.toByteArray();

     JSONObject fullCommand = new JSONObject();
     fullCommand.put("command", ServiceProtocol.UPDATE_IMAGE.getAbbr());
     JSONArray jImg = new JSONArray(data);

     fullCommand.put("img", jImg);
     ServerThread.server.sendMessageTo(socket, fullCommand.toString());


 } catch (SQLException e) {
     e.printStackTrace();
 } catch (IOException e) {
     e.printStackTrace();
 } catch (JSONException e) {
     e.printStackTrace();
 }
        */
    }

    public void updateImage(JSONObject object) {
        try {
            JSONArray jImg = object.getJSONArray("argument");
            int id = object.getInt("id");
            byte[] img = new byte[jImg.length()];
            for (int i = 0; i < jImg.length(); i++) {
                img[i] = (byte) jImg.getInt(i);
            }
            dao.setImage(img, id);

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

    public void connectDB() throws SQLException {
        PersonDao p = new PersonDao();
        persons.clear();
        p.getAll(persons);
    }
}
